#!/bin/bash
set -- `getopt "abcnrd" "$@"` || {
	#debugging pour moi:
    echo "Usage: `run_ps.sh $0` [-a] [-b] [-c] [-n] [-r] [-d]" 1>&2
    exit 1
}

airport=0 battery=0 cpu=0 network=0 disk=0

while :
do
    case "$1" in
    -a) airport=1 ;;
    -b) battery=1 ;;
    -c) cpu=1 ;;
    -n) network=1 ;;
    -r) cpu=1 ;;
    -d) disk=1 ;;
    --) break ;;
    esac
    shift
done
shift    # REMOVE THE TRAILING --
#echo "airport=$airport / battery=$battery / cpu=$cpu / network=$network / disk=$disk"

#note that we always get cpu and ram together, even if they're not both being monitored

if [ $cpu = 1 ]; then
	#use ps because top takes quite a heavy hit:
	/bin/ps auxc | /usr/bin/awk '{n++;c+=$3; r+= $4};END{printf("%3.2f,%3.2f", c, r)}'
else
	printf "0,0";
fi

#now network:
if [ $network = 1 ]; then
	#
	printf ",0"
else
	printf ",0";
fi

#now battery:
if [ $battery = 1 ]; then
	#this is actually a variation on a script I found on google some time ago - I'll
	#credit the original author when I find him... for now: cheers mate!
	/usr/sbin/ioreg -p IODeviceTree -n "battery" -w 0 | awk '/IOBatteryInfo/ {info = $6 $7; split(info, bat, ","); printf(",%3.2f", substr(bat[4], index(bat[4],"=") + 1,10) / substr(bat[1], index(bat[1],"=") + 1,10) * 100)}'
	
else
	printf ",0";
fi

#and now airport status:
if [ $airport = 1 ]; then
	#this is using a command line tool i seen on google that we edited. Full credit to the 
	#original author to be given soon
	./airport | /usr/bin/awk '{printf(",%3d",$1)}'  
	#| /usr/bin/awk 'NR == 2 {sig_strength=$1; printf(",%3d", substr(sig_strength, 0, index(sig_strength,"%")))}'
else
	printf ",0";
fi

#and now disk usage:
if [ $disk = 1 ]; then
	#Just use df to see the capacity for the startup disk (/)
	/bin/df -l | awk 'NR == 2 {capacity = $5; printf(",%3d\n", substr(capacity, 0, index(capacity,"%")))}' 

else
	printf ",0";
fi